package com.qg.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;

import com.qg.dto.Dto;

import com.qg.exception.GoodsException;

import com.qg.module.pojo.QgUser;
import com.qg.module.vo.GoodsVo;
import com.qg.service.LocalGoodsService;
import com.qg.service.QgGoodsService;
import com.qg.service.QgGoodsTempStockService;
import com.qg.utils.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.qg.exception.GoodsException.USER_REPEAT_GET;

@RequestMapping("/api")
@Controller
public class GoodsController {

    @Autowired
    private LocalGoodsService localGoodsService;

    @Autowired
    private RedisUtil redisUtil;


    /***
     * 需求:获取商品信息，获取库存信息
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/v/queryGoodsById",method = RequestMethod.POST)
    public Dto queryGoodsById(String id) throws Exception {
        return localGoodsService.queryGoodsById(id);
    }
    /***
     * 抢购商品的方法
     * @param token
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/v/getGoods",method = RequestMethod.POST)
    public Dto getGoods(String token,String goodsId)throws Exception{

        
         String userJson=redisUtil.getStr(token);
        //不是空
        if(EmptyUtils.isNotEmpty(userJson)){
            //转换为用户信息
            QgUser qgUser= JSONObject.parseObject(userJson,QgUser.class);
            return localGoodsService.sendQGMassage(qgUser.getId(),goodsId);
        }else{//是空
        return DtoUtil.returnFail("用户登录失效！",Constants.USER_LOGIN_LOSE);
        }

    }


    /**
     * 前端轮询检查状态
     * @param token
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/v/flushStatus",method = RequestMethod.POST)
    public Dto flushStatus(String token,String goodsId)throws Exception{
        return  localGoodsService.flushMassage(token,goodsId);
    }
}
